package com.wx.cloud.daily.service.impl;

import com.wx.cloud.daily.mapper.DailyMapper;
import com.wx.cloud.daily.model.Daily;
import com.wx.cloud.daily.service.DailyService;
import com.wx.cloud.daily.utils.MyDate;
import com.wx.cloud.daily.utils.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("dailyService")
public class DailyServiceImpl implements DailyService {

    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private WordUtil wordUtil;

    @Override
    public List<Daily> getMyDailyList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return dailyMapper.getMyDailyList(data);
    }

    @Override
    public int getMyDailyCount(Map<String, Object> data) {
        return dailyMapper.getMyDailyCount(data);
    }

    @Override
    public List<Daily> getSelectList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return dailyMapper.getSelectList(data);
    }

    @Override
    public int getSelectCount(Map<String, Object> data) {
       return dailyMapper.getSelectCount(data);
    }

    @Override
    public Daily getDailyById(Integer id) {
        return dailyMapper.getDailyById(id);
    }

    @Override
    public String saveDaily(Daily daily) {
        int result = dailyMapper.saveDaily(daily);
        if (result >0 ){
            return "success";
        }
        return "false";
    }

    @Override
    public String submitDaily(Daily daily) {
        int result = dailyMapper.submitDaily(daily);
        if (result >0 ){
            return "success";
        }
        return "false";
    }

    @Override
    public int saveCheckDaily(Daily daily) {
        daily.setUpdateTime(MyDate.getDate());
        return dailyMapper.saveCheckDaily(daily);
    }

    @Override
    public int submitCheckDaily(Daily daily) {
        daily.setUpdateTime(MyDate.getDate());
        return dailyMapper.submitCheckDaily(daily);
    }

    @Override
    public int checkDaily(Daily daily) {
        daily.setUpdateTime(MyDate.getDate());
        return dailyMapper.checkDaily(daily);
    }

    @Override
    public void createWord(Daily daily) {
        wordUtil.createWord(daily);
    }

    @Override
    public int delDaily(Daily daily) {
        daily.setUpdateTime(MyDate.getDate());
        return dailyMapper.delDaily(daily);
    }

    @Override
    public String batchDeletion(List<Integer> ids) {
        Map params = new HashMap();
        params.put("ids",ids);
        params.put("updateTime",MyDate.getDate());
        int result = dailyMapper.batchDeletion(params);
        if (result>0){
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public void createDeletion(List<Integer> ids) {
        Map params = new HashMap();
        params.put("ids",ids);
        List<Daily> dailies = dailyMapper.getDailyByIds(params);
        for (Daily daily : dailies) {
            if (!daily.getStateCode().equals("draft")){
                wordUtil.createWord(daily);
            }
        }
    }

}
